// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DBMarketHelpers.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.Entities
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Entities;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The db market helpers.
    /// </summary>
    public partial class DBMarketHelpers
    {
        #region Public Methods

        /// <summary>
        /// The create market.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <returns>
        /// </returns>
        public static IMarket CreateMarket(Guid dictionaryId, string path)
        {
            var dbDictionary = DBDictionaryServiceHelpers.GetDBDictionaryService(dictionaryId);
            if (dbDictionary != null)
            {
                var o = new TimeSpan(9, 0, 0);
                var c = new TimeSpan(17, 0, 0);

                var dbMarket = DBMarket.CreateDBMarket(Guid.NewGuid(), o, c, o, c, o, c, o, c, o, c, o, c);
                dbMarket.Path = PathUtil.Cleanup(path);
                dbDictionary.DBMarket.Add(dbMarket);

                if (Singleton<DB>.Instance.Commit() > 0)
                {
                    return new Market(dbMarket);
                }
            }

            return null;
        }

        /// <summary>
        /// The get db market.
        /// </summary>
        /// <param name="marketId">
        /// The marketId.
        /// </param>
        /// <returns>
        /// </returns>
        public static DBMarket GetDBMarket(Guid marketId)
        {
            var dbMarkets = from d in Singleton<DB>.Instance.Entities.DBMarket where d.ID == marketId select d;

            if (dbMarkets.Count() == 1)
            {
                return dbMarkets.First();
            }

            return null;
        }

        /// <summary>
        /// The get db market.
        /// </summary>
        /// <param name="dbDictionary">
        /// The db dictionary.
        /// </param>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <returns>
        /// </returns>
        public static DBMarket GetDBMarket(DBDictionaryService dbDictionary, string path)
        {
            var dbMarkets = from m in dbDictionary.DBMarket where m.DBDictionaryService.ID == dbDictionary.ID && m.Path == path select m;

            if (dbMarkets.Count() == 1)
            {
                return dbMarkets.First();
            }

            return null;
        }

        /// <summary>
        /// The load markets.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <returns>
        /// </returns>
        public static IMarket[] LoadMarkets(Guid dictionaryId)
        {
            var dbMarkets = from m in Singleton<DB>.Instance.Entities.DBMarket where m.DBDictionaryService.ID == dictionaryId select m;

            if (dbMarkets.Count() <= 0)
            {
                return new IMarket[] { };
            }

            IList<IMarket> markets = new List<IMarket>();
            foreach (var dbMarket in dbMarkets)
            {
                markets.Add(new Market(dbMarket));
            }

            return markets.ToArray();
        }

        #endregion
    }
}